﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Common;
using HtmlAgilityPack;
using HtmlHelper;

namespace biquge5200.Common
{
    /// <summary>
    /// 获取网页中的列表
    /// </summary>
    public class GetList
    {
        /// <summary>
        /// 获取页面中的所有小说
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static List<Menus> GetAllFiction(HtmlDocument doc)
        {
            var arrayList = new List<Menus>();
            try
            {
                HtmlNodeCollection hrefs = doc.SelectNode("//li/a[@href]");
                if (hrefs == null)
                    return null;
                foreach (HtmlAgilityPack.HtmlNode href in hrefs)
                {
                    string text = HtmlHelper.HtmlNode.InnerText(href);
                    string hreff = HtmlHelper.HtmlNode.GetNodeAttribute(href, "href").Value;
                    arrayList.Add(new Menus() { Text = text, Href = hreff });
                }
            }
            catch (Exception)
            {
                // ignored
            }
            return arrayList.ToArray().ToList();
        }
        /// <summary>
        /// 获取章节目录
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static List<Menus> GetMenu(HtmlDocument doc)
        {
            var arrayList = new List<Menus>();
            try
            {
                HtmlNodeCollection hrefs = doc.SelectNode("//dd/a[@href]");
                if (hrefs == null)
                    return null;
                foreach (HtmlAgilityPack.HtmlNode href in hrefs)
                {
                    string text = HtmlHelper.HtmlNode.InnerText(href);
                    string hreff = HtmlHelper.HtmlNode.GetNodeAttribute(href, "href").Value;
                    arrayList.Add(new Menus() { Id = Helper.ConvertTitle(text), Text = text, Href = hreff });
                }
            }
            catch (Exception)
            {
                // ignored
            }
            return arrayList.Distinct().OrderByDescending(o=>o.Id).ToList();
        }
    }
    /// <summary>
    /// 目录信息
    /// </summary>
    public class Menus
    {
        /// <summary>
        /// 章节编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 目录文本
        /// </summary>
        public string Text { get; set; }
        /// <summary>
        /// 目录链接
        /// </summary>
        public string Href { get; set; }
    }
}
